<?php 
class AdBrand extends Object
{
	public $AdsCount;
	public $TypeId;
	public $CatId;
	public $BrandOrder;
	public $Title;
	public $Code;
	public $Description;
	public $Image;
	public $ImageObject;
	public $Active;
	
	public function AdBrand($values = '')
	{ 
		self::setDataTable('am_brands');
		if(is_array($values))
		{ self::setValues($values); }
	}
	
	public function getAdsCount() { return intval($this->AdsCount); }
	public function getTypeId() { return intval($this->TypeId); }
	public function getCatId() { return intval($this->CatId); }
	public function getBrandOrder() { return intval($this->BrandOrder); }
	public function getImageURL() { return ADBRAND_URL.self::getImage(); }
	public function getImageThumbURL() { return ADBRAND_URL.'thumbs/'.self::getImage(); }
	
	public function setAdsCount($val) { $this->AdsCount=intval($val); }
	public function setTypeId($val) { $this->TypeId=intval($val); }
	public function setCatId($val) { $this->CatId=intval($val); }
	public function setBrandOrder($val) { $this->BrandOrder=intval($val); }
	public function loadMetaData()
	{
		global $katana;
		global $txt;
		
		$katana->setDetailView(false);
		if(self::getDescription() != '')
		{ $katana->setMetaData(self::getDescription()); }
		$katana->addBeforeMetaKey(self::getTitle());
	}
	
	function update()
	{
		if((self::getId() > 0) && (self::getDataChanged()))
		{
			$sql  = 'UPDATE '.DB_PERFIX.self::getDataTable().' SET ';
			$sql .= '`typeId`='.self::getTypeId().', ';
			$sql .= '`catId`='.self::getCatId().', ';
			$sql .= '`BrandOrder`='.self::getBrandOrder().', ';
			$sql .= '`title`="'.self::formatText(self::getTitle()).'", ';
			$sql .= '`Code`="'.self::formatText(self::getCode()).'", ';
			$sql .= '`Description`="'.self::formatText(self::getDescription()).'", ';
			$sql .= '`image`="'.self::formatText(self::getImage()).'", ';
			$sql .= '`active`='.self::getActive().' ';
			$sql .= ' WHERE id='.self::getId().' LIMIT 1;';
			mysql_query($sql);
		}
	}
	
	function insert()
	{
		global $adc;
		$id = 0;
		if(self::getTitle() != '')
		{
			$insert .= '`title`';
			$values .= '"'.self::formatText(self::getTitle()).'"';
			
			$insert .= ', `Description`';
			$values .= ', "'.self::formatText(self::getDescription()).'"';
				
			$insert .= ', `typeId`';
			$values .= ', '.self::getTypeId();
			
			$insert .= ', `catId`';
			$values .= ', '.self::getCatId();
			
			$sql  = 'INSERT INTO '.DB_PERFIX.self::getDataTable().' ('.$insert.') '; 
			$sql .= 'VALUES ('.$values.');';
			$result = mysql_query($sql);
			
			$sql  = 'SELECT id FROM '.DB_PERFIX.self::getDataTable().' WHERE `title`="'.self::formatText(self::getTitle()).'" ';
			$sql .= 'ORDER BY id DESC LIMIT 1;';
			$result = mysql_query($sql);
			
			if(mysql_num_rows($result) > 0)
			{
				while($row = mysql_fetch_array($result))
				{ 
					self::setId($row['id']); 
					$id = $row['id'];
				}
			}
			$adc->setTTL(0);
		}
		return $id;
	}
	
	function uploadImage($file)
	{
		global $thumb;
		self::setImage(self::uploadObjectImage(ADBRAND_PATH, $file, $thumb['ADBRAND'], self::getCode()));
	}
	
	function deleteImage()
	{
		global $thumb;
		self::setImage(self::deleteObjectImage(ADBRAND_PATH, self::getImage(), $thumb['ADBRAND']));
	}
}
?>